package leetcode.Leetcode3;

import java.util.HashMap;
import java.util.Iterator;

class DraftSolution {
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        int len=s.length();
        int i = 0, j = 0;
        int maxLen = 0;
        while (i < len) {
            Character character = s.charAt(i);
            if (!map.containsKey(character)) {
                map.put(character, i);
                int currLen = i - j + 1;
                maxLen = Math.max(currLen, maxLen);
                i++;
            } else if (map.containsKey(character)){
                int pos= map.get(character);
                j = pos+1;
                map.put(character, i);
                for(Iterator<Character> iterator = map.keySet().iterator(); iterator.hasNext(); ) {
                    Character key = iterator.next();
                    if(map.get(key)<j) {
                        iterator.remove();
                    }
                }
                i++;
            }
        }
        System.out.println(maxLen);
        return maxLen;
    }

    public static void main(String[] args) {
        DraftSolution draftSolution = new DraftSolution();
        draftSolution.lengthOfLongestSubstring("bbbb");
    }
}